package com.group01.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.group01.community.entity.*;
import com.group01.community.entity.vo.ZyCommunityInteractionVO;
import com.group01.community.service.ZyCommentService;
import com.group01.community.service.ZyCommunityInteractionService;
import com.group01.community.service.ZyFilesService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 社区互动表 前端控制器
 * </p>
 *
 * @author xuxu
 * @since 2025-05-01
 */
@Api(tags = "社区互动信息")
@RestController
@RequestMapping("/zyCommunityInteraction")
public class ZyCommunityInteractionController {
    public static final String DIR = "http://http://192.168.110.250//";

    @Autowired
    private ZyCommunityInteractionService zyCommunityInteractionService;

    @Autowired
    private ZyCommentService zyCommentService;

    @Autowired
    private ZyFilesService zyFilesService;

    @Autowired
    private FastFileStorageClient client;

    /**
     *
     *
     * @param pageCurrent
     * @param pageSize
     * @param zyCommunityInteractionVO
     * @param communityName
     * @return
     */
    @ApiOperation("社区互动分页加模糊查询")
    @GetMapping("/page")
    public ResponseResult<IPage<ZyCommunityInteractionVO>> getAllZyCommunityInteractionVO(Long pageCurrent, Long pageSize, ZyCommunityInteractionVO zyCommunityInteractionVO,String communityName){
        IPage<ZyCommunityInteractionVO> page = zyCommunityInteractionService.selectAllZyCommunityInteractionVO(pageCurrent, pageSize, zyCommunityInteractionVO, communityName);
        return ResponseResult.ok(page);
    }

    /**
     * 获取所有的图片
     * @return      图片的集合
     */
    @GetMapping("/getAllImg")
    public ResponseResult<List<ZyFiles>> getAllImg(){
        List<ZyFiles> list = zyFilesService.list();
        return ResponseResult.ok(list);
    }

    /**
     * 获取前四条热门社区互动信息
     * @return          ZyCommunityInteractionVO的集合
     */
    @GetMapping("getHotInteraction")
    public ResponseResult<List<ZyCommunityInteractionVO>> getHotInteraction(){
        return ResponseResult.ok(zyCommunityInteractionService.getHotInteraction());
    }


    /**
     * 上传头像
     * @param file  头像
     * @return      储存地址
     * @throws IOException
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ResponseEntity<String> upload(MultipartFile file) throws IOException {
        //获得后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传
        StorePath storePath = client.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        return ResponseEntity.ok(DIR + storePath.getFullPath());
    }

    /**
     * 添加社区互动信息
     * @param zyCommunityInteraction        社区互动信息对象
     * @return                              ok
     */
    @PostMapping("/addInteraction")
    public ResponseResult<String> addInteraction(ZyCommunityInteractionVO zyCommunityInteraction){
        long interactionId = UUIDUtils.getId();
        zyCommunityInteraction.setInteractionId(interactionId);
        zyCommunityInteraction.setCreateTime(LocalDateTime.now());

        zyCommunityInteractionService.save(zyCommunityInteraction);

        String url = zyCommunityInteraction.getOwnerNickname();
        String[] urls = url.split(",");
        ZyFiles zyFiles = new ZyFiles();
        for (int i = 1; i < urls.length; i++) {
            zyFiles.setFilesId(UUIDUtils.getId());
            zyFiles.setFilesUrl(urls[i]);
            zyFiles.setCreateTime(LocalDateTime.now());
            zyFiles.setParentId(interactionId);
            zyFiles.setUserId(zyCommunityInteraction.getUserId());
            zyFiles.setRemark("CommunityInteraction");
            zyFilesService.save(zyFiles);
        }

        return ResponseResult.ok("ok");
    }

    /**
     * 添加社区互动评论
     * @param zyComment        评论对象
     * @return                 ok
     */
    @PostMapping("addComment")
    public ResponseResult<String> addComment(ZyComment zyComment){
        zyComment.setCreateTime(LocalDateTime.now());
        zyCommentService.save(zyComment);
        return ResponseResult.ok("ok");
    }


}

